package com.spark.actions.fin.gld;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.session.SqlSession;

import com.spark.actions.sys.DefaultAction;
import com.spark.core.SparkContext;
import com.spark.core.SparkException;
import com.spark.dao.SessionFactory;
import com.spark.tools.Logger;

@SuppressWarnings("unchecked")
public class AuxiliaryItemAction extends DefaultAction {

	
	private static final String SQL_GLD_ACCOUNT_SELECT_AUX = "gld_accounts.selectAuxiliaryItemByPrimaryKey";
	private static final String SQL_GLD_AUXILIARY_SELECT = "gld_account_auxiliary_item.selectByItemCodes";
	
	
	private static final String ERROR_CODE_AUXILIARY_SELECT_BY_CODES = "AUXILIARY_SELECT_BY_CODES";
	
	/**
	 * 查询科目核算项
	 * @param context
	 * @throws SparkException
	 */	
	public void selectAccountAux(SparkContext context) throws SparkException{
		Map<String, Object> model = context.getModel();
		SqlSession sqlSession = SessionFactory.getSession();
		try {
			HttpServletRequest request = context.getRequest();
			String id = request.getParameter("account_id");
			if(id == null)return;
			Integer accountId = Integer.valueOf(id);
			String auxStr = (String)sqlSession.selectOne(SQL_GLD_ACCOUNT_SELECT_AUX, accountId);
			if(auxStr!=null) {
				Map map = new HashMap();
				initModel(context, map);
//				List list = new ArrayList();
				String[] auxs = auxStr.split("/");
				map.put(LIST, auxs);
//				for(int i=0,l=auxs.length;i<l;i++){
//					
//				}
				List auxList = (List)sqlSession.selectList(SQL_GLD_AUXILIARY_SELECT, map);
				model.put("auxList", auxList);
			}
		} catch (NumberFormatException e){
			return;
		} catch (Exception e){
			Integer errorId = Logger.log(ERROR_CODE_AUXILIARY_SELECT_BY_CODES, context, e);
			throw new SparkException(errorId);
		} finally {
			SessionFactory.closeSession(sqlSession);
		}		
	}
}
